using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Api.Services;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// 编码规则元数据控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class CodeRuleMetadataController : ControllerBase
    {
        private readonly ICodeRuleMetadataService _service;

        public CodeRuleMetadataController(ICodeRuleMetadataService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取业务名称列表(带实体限定名)
        /// </summary>
        [HttpGet("businesses")]
        public async Task<ApiResult<List<BusinessDto>>> GetBusinesses()
        {
            var data = await _service.GetBusinessesAsync();
            return new ApiResult<List<BusinessDto>>
            {
                Code = ApiEnum.Success,
                Msg = "查询成功",
                Data = data
            };
        }

        /// <summary>
        /// 获取指定实体的字段列表
        /// </summary>
        [HttpGet("fields")]
        public async Task<ApiResult<List<FieldDto>>> GetFields([FromQuery] string classFullName)
        {
            if (string.IsNullOrWhiteSpace(classFullName))
            {
                return new ApiResult<List<FieldDto>>
                {
                    Code = ApiEnum.Failed,
                    Msg = "classFullName 必填"
                };
            }

            var data = await _service.GetFieldsAsync(classFullName);
            return new ApiResult<List<FieldDto>>
            {
                Code = ApiEnum.Success,
                Msg = "查询成功",
                Data = data
            };
        }

        /// <summary>
        /// 获取可作为日期排序字段的列表(创建时间/更新时间)
        /// </summary>
        [HttpGet("date-fields")]
        public async Task<ApiResult<List<FieldDto>>> GetDateFields([FromQuery] string classFullName)
        {
            if (string.IsNullOrWhiteSpace(classFullName))
            {
                return new ApiResult<List<FieldDto>>
                {
                    Code = ApiEnum.Failed,
                    Msg = "classFullName 必填"
                };
            }

            var data = await _service.GetDateFieldsAsync(classFullName);
            return new ApiResult<List<FieldDto>>
            {
                Code = ApiEnum.Success,
                Msg = "查询成功",
                Data = data
            };
        }
    }
}